package com.kb.controller;

import cn.hutool.core.map.MapUtil;
import com.kb.annotation.UserLoginToken;
import com.kb.common.ApiResponse;
import com.kb.common.MeException;
import com.kb.entity.CallbackNotification;
import com.kb.entity.ModelApplication;
import com.kb.service.CallbackNotificationService;
import com.kb.util.GCutil;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/resource")
public class GcResourceDataController {

    @Resource
    private CallbackNotificationService callbackNotificationService;
    /**
     * 回调通知
     */
    @PostMapping("/callbackNotification")
    @UserLoginToken
    public ApiResponse callbackNotification(@RequestBody List<CallbackNotification> callbackNotificationList) throws MeException {
        callbackNotificationService.callbackNotification(callbackNotificationList);
        return ApiResponse.success("ok");
    }

    /**
     * 获取token
     */
    @GetMapping("/getToken")
    public ApiResponse getToken() throws MeException {
        String token = GCutil.getToken();
        return ApiResponse.success(token);
    }
    /**
     * 获取token
     */
    @GetMapping("/getLocalGcToken")
    public ApiResponse getLocalGcToken() throws MeException {
        String token = GCutil.getLocalGcToken();
        return ApiResponse.success(token);
    }
    /**
     * 获取token
     */
    @GetMapping("/getAccessToken")
    public ApiResponse getAccessToken() throws MeException {
        String token = GCutil.getToken();
        String bearer = token.replace("Bearer ", "").trim();
        HashMap<Object, Object> objectObjectHashMap = MapUtil.newHashMap();
        objectObjectHashMap.put("token", bearer);
        return ApiResponse.success(objectObjectHashMap);
    }
    /**
     * 下载文件
     */
    @ResponseBody
    @GetMapping("/getFile")
    public ApiResponse getFile(String id) throws MeException {
        callbackNotificationService.getFile(id,"DataSheets");
        return ApiResponse.success("ok");
    }
    /**
     * 批量获取信息 id
     */
    @ResponseBody
    @GetMapping("/batchGet")
    public ApiResponse batchGet(String ids) throws MeException {
        callbackNotificationService.batchGet(ids,null);
        return ApiResponse.success("ok");
    }
    /**
     * 获取厂商信息和分类信息
     */
    @ResponseBody
    @GetMapping("/getFactoryInfo")
    @XxlJob("getFactoryInfo")
    public ApiResponse getFactoryInfo() throws MeException {
        callbackNotificationService.getFactoryInfo();
        return ApiResponse.success("ok");
    }
    /**
     * 新模型申请接口
     */
    @PostMapping("/modelApplication")
    public ApiResponse modelApplication( @RequestBody ModelApplication modelApplication) throws MeException {
        return ApiResponse.success(callbackNotificationService.modelApplication(modelApplication));
    }
    /**
     * 定时拉取数据更新
     */
    @PostMapping("/scheduleUpdate")
    @XxlJob("scheduleUpdate")
    public ApiResponse<?> scheduleUpdate(@RequestBody List<CallbackNotification> list) throws MeException {
        return ApiResponse.success(callbackNotificationService.scheduleUpdate(list));
    }

    /**
     * 下载封装格式
     */
    @GetMapping("/downloadFile")
    @XxlJob("downloadFile")
    public ApiResponse downloadFile() throws MeException {
        callbackNotificationService.downloadFile();
        return ApiResponse.success("完成");
    }
}
